from django.core.management.base import BaseCommand
from django.contrib.auth.models import Group,Permission,ContentType
from apps.news.models import News,NewsCategory,Banner,Comment
from apps.course.models import Course,Category

class Command(BaseCommand):

    def handle(self,*args,**options):
        """
        1.编辑组
        2.财务组（管理课程订单/付费资讯订单
        3.管理员组（编辑组+财务组）
        4.角色：超级管理员
        """

        # 1.编辑组
        edit_content_types = [
            ContentType.objects.get_for_model(News),
            ContentType.objects.get_for_model(NewsCategory),
            ContentType.objects.get_for_model(Banner),
            ContentType.objects.get_for_model(Comment),
            ContentType.objects.get_for_model(Course),
            ContentType.objects.get_for_model(Category),
        ]
        # self.stdout.write(self.style.SUCCESS('hello world'))
        edit_permissions = Permission.objects.filter(content_type__in=edit_content_types)
        editGroup = Group.objects.create(name='自建编辑')
        editGroup.permissions.set(edit_permissions)
        editGroup.save()
        self.stdout.write(self.style.SUCCESS('编辑组创建完成'))

        # # 2.财务组
        # finance_content_types = [
        #     ContentType.objects.get_for_model(News),
        #     ContentType.objects.get_for_model(NewsCategory),
        #     ContentType.objects.get_for_model(Banner),
        #     ContentType.objects.get_for_model(Comment),
        #     ContentType.objects.get_for_model(Course),
        #     ContentType.objects.get_for_model(Category),
        # ]
        # self.stdout.write(self.style.SUCCESS('hello world'))
        # finance_permissions = Permission.objects.filter(content_type__in=edit_content_types)
        # financeGroup = Group.objects.create(name='财务组')
        # financeGroup.permissions.set(finance_permissions)
        # financeGroup.save()

        # 3.管理组
        # admin_permissions = edit_permissions.union(finance_permissions)
        # adminGroup = Group.objects.create(name='管理员')
        # adminGroup.save()

        # 4.超级管理员
        self.stdout.write(self.style.SUCCESS('超级管理员创建完成'))